exports.createUserFolder = function () {
  console.info('创建默认目录')
  let fs = require('fs')
  // fs.mkdirSync('E:\\learn\\electron\\file-manager\\data-test1')
  let userDataPath = require('path').join(process.cwd(), '../file-manager-data/data')
  if (!fs.existsSync(userDataPath)) {
    fs.mkdirSync(userDataPath, {recursive: true})
  } else {
    console.info('文件夹已存在', userDataPath)
  }
  let userToolDataPath = require('path').join(process.cwd(), '../file-manager-data/data/tool')
  if (!fs.existsSync(userToolDataPath)) {
    fs.mkdirSync(userToolDataPath, {recursive: true})
  } else {
    console.info('工具文件夹已存在', userToolDataPath)
  }
  console.info('userDataPath', userDataPath)
}

exports.userFolder = function () {
  return require('path').join(process.cwd(), '../file-manager-data/data')
}

exports.createDb = function () {
  console.info('初始化创建表')
  var sqlite3 = require('sqlite3').verbose();// eslint-disable-line
  // sqlites数据库地址
  let sqliteDbPath = exports.userFolder() + '\\manager_file.db'
  console.info('数据库路径为:{}', sqliteDbPath)
  // 打开sqlites数据库
  let db = new sqlite3.Database(sqliteDbPath)// eslint-disable-line
  let createFileSql = 'CREATE TABLE if not exists t_file (id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,file_name VARCHAR(300)  NULL,file_path VARCHAR(300)  NULL,suffix VARCHAR(10)  NULL,remark VARCHAR(3000)  NULL,create_time TIMESTAMP DEFAULT (datetime(CURRENT_TIMESTAMP,\'localtime\')) NULL,create_user VARCHAR(30)  NULL,update_time timesTAMP  NULL,update_user VARCHAR(30)  NULL,del_flag INTEGER  NULL)'
  // let createFileSql = 'create table if not exists labels(level INTEGER, longitude REAL, latitude REAL, content BLOB);'
  try {
    db.run(createFileSql, function (err) {
      if (err) {
        console.error('创建数据库异常：', err)
        throw err
      } else {
        console.info('创建表成功')
        return db
      }
    })
  } catch (e) {
    console.error('创建异常：', e)
  }

  let createToolSql = 'CREATE TABLE  if not exists [t_tool] ([id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,[title] varCHAR(30)  NULL,[path] vaRCHAR(300)  NULL,[icon] vaRCHAR(300)  NULL,[type] inTEGER  NULL,[tool_index] inTEGER  NULL,[remark] vaRCHAR(500)  NULL,[create_time] TIMESTAMP  NULL,[update_time] timesTAMP  NULL)'
  // let createFileSql = 'create table if not exists labels(level INTEGER, longitude REAL, latitude REAL, content BLOB);'
  try {
    db.run(createToolSql, function (err) {
      if (err) {
        console.error('创建工具表异常：', err)
        throw err
      } else {
        console.info('创建创建工具表成功')
        return db
      }
    })
  } catch (e) {
    console.error('创建异常：', e)
  }
}
exports.getDB = function queryData1 () {
  console.info('查询')
  var sqlite3 = require('sqlite3').verbose();// eslint-disable-line
  // sqlites数据库地址
  let sqliteDbPath = exports.userFolder() + '\\manager_file.db'
  console.info('数据库路径为:{}', sqliteDbPath)
  // 打开sqlites数据库
  let db = new sqlite3.Database(sqliteDbPath)// eslint-disable-line
  let createFileSql = 'CREATE TABLE if not exists t_file (id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,file_name VARCHAR(300)  NULL,file_path VARCHAR(300)  NULL,suffix VARCHAR(10)  NULL,remark VARCHAR(3000)  NULL,create_time TIMESTAMP DEFAULT (datetime(CURRENT_TIMESTAMP,\'localtime\')) NULL,create_user VARCHAR(30)  NULL,update_time timesTAMP  NULL,update_user VARCHAR(30)  NULL,del_flag INTEGER  NULL)'
  // let createFileSql = 'create table if not exists labels(level INTEGER, longitude REAL, latitude REAL, content BLOB);'
  try {
    db.run(createFileSql, function (err) {
      if (err) {
        console.error('创建数据库异常：', err)
        throw err
      } else {
        console.info('创建表成功')
        return db
      }
    })
  } catch (e) {
    console.error('创建异常：', e)
  }
  return db
}
